{
	_type:"controller",
	_config_details: {	
	},
	_config: {
		path:"_auto_admin/{{mconfig.path}}",
		name:"AutoAdmin{{mconfig.name}}",
		admin:1,
		auto:1,
		role_code:"{{mconfig.adminpanel.role}}"
	},
	{{if mconfig.adminpanel.section}}
	_admin_nav: [
		{section:"{{mconfig.adminpanel.section}}",name:"{{mconfig.adminpanel.label}}",mode:{{unless mconfig.adminpanel.prehelperfunc}}"index"{{/unless}}{{if mconfig.adminpanel.prehelperfunc}}"pre"{{/if}}}
	],
	{{/if}}
	{{if prehelper}}
	pre: function(config) {
		var html={{prehelper}}
		if (html) return html;
		var c=this._cinfo.config;
		return this.views.process("controllers/admin/"+c.name+"/"+(c[this._cinfo.mode+"_view"]||this._cinfo.mode));
	},
	{{/if}}
	index: function(config) {
		this.items=this.site.models.{{mconfig.name}}.{{listfunc}}("{{listname}}");
		{{listhelper}}
		return this.Cview();
	},

	edit: function(config) {
		if (this.fields.id) {
			this.item=this.site.models.{{mconfig.name}}.Get(this.fields.id);
		} else {
			this.item=this.site.models.{{mconfig.name}}.Create();
			this.item.ApplyAdminFields(this.fields);
			this.item.enabled=1;
		}
		this.dictionaries={};
		{{foreach d in dictionaries}}this.dictionaries.{{d.table}}=this.site.models.{{d.model}}.ListFK();
		{{/foreach}}
		{{if mconfig.columns_l10n.length}}this.item.FetchLocales();
		this.F("Admin","load_l10ns");
		{{/if}}
		this.item.FetchFileFolderFiles();
		{{edithelper}}
		return this.Cview();
	},

	reorder: function(config) {
		this.site.models.{{mconfig.name}}.Reorder(this.fields.ordering.replace(/\w+-(\d+)/g,function(p,p1) { return p1; }).split(/,/));
		return JSON.stringify({});
	},

	save: function(config) {
		var item;
		if (this.fields.id) {
			item=this.site.models.{{mconfig.name}}.Get(this.fields.id);
		} else {
			item=this.site.models.{{mconfig.name}}.Create();
		}
		{{if mconfig.columns_l10n.length}}item.FetchLocales();
		this.F("Admin","load_l10ns");
		{{/if}}
		item.ApplyAdminFields(this.fields);
		if (!item.id) item.user_cr_id=this.uid;
		item.SaveAll();
		this.item=item;
		{{savehelper}}
		return JSON.stringify({redirect:{url:this.action+this.F("Admin","cols_through_params_full",{{JSON:mconfig.adminpanel.cols_through}})}});
	},

	"save_only": function(config) {
		var ret=this.C("AutoAdmin{{mconfig.name}}","save");
		if (ret.substr(0,10)=='{"redirect') return JSON.stringify({call:"ajajform_frm_set_id",name:"id",id:this.item.id});
		return ret;
	},

	"delete": function(config) {
		this.item=this.site.models.{{mconfig.name}}.Get(this.fields.id);
		this.dictionaries={};
		{{foreach d in dictionaries}}this.dictionaries.{{d.table}}=this.site.models.{{d.model}}.ListFK();
		{{/foreach}}
		{{if mconfig.columns_l10n.length}}this.item.FetchLocales();
		this.F("Admin","load_l10ns");
		{{/if}}
		this.cols_through=this.F("Admin","cols_through_params_part",{{JSON:mconfig.adminpanel.cols_through}});
		this.item.FetchFileFolderFiles();
		return this.Cview();
	},

	delete2: function(config) {
		var item=this.site.models.{{mconfig.name}}.Get(this.fields.id);
		{{if mconfig.columns_l10n.length}}item.FetchLocales();
        this.F("Admin","load_l10ns");
        {{/if}}
        item.ApplyAdminFields(this.fields);
        item.Delete();
		return JSON.stringify({redirect:{url:this.action+this.F("Admin","cols_through_params_full",{{JSON:mconfig.adminpanel.cols_through}})}});
	},

	import: function(config) {
		return this.Cview();
	},
	
	import2: function(config) {
		var data=this.req.files["file-file"].data;
		var table;
		switch (this.fields.filetype) {
			case "xlsx":
				table=this.F("Admin","xlsx_to_json_table",data);
				break;
			case "xml":
				table=this.F("Admin","xml_to_json_table",data);
				break;
			case "json":
				table=JSON.parse(data.toString("utf-8"));
				data="";
				break;
		}
		this.html=this.F("Admin","import_data",this.site.models.{{mconfig.name}},{
			fk:this.fields.fk,
			old:this.fields.old,
			helper:{{JSON:mconfig.adminpanel.impexphelperfunc}}
		},table);
		if (this.fields.ajaj==1) return JSON.stringify({done:{msg:this.html,title:"Import completed"}});
		return this.Cview();
	},

	export: function(config) {
		return this.Cview();
	},

	export2: function(config) {
		var table=this.F("Admin","export_data",this.site.models.{{mconfig.name}},{
			fk:this.fields.fk,
			helper:{{JSON:mconfig.adminpanel.impexphelperfunc}}
		});
	
		switch (this.fields.filetype) {
			case "json":
				this.jsng_response.response_headers["Content-type"]="text/json";
				this.jsng_response.response_headers["Content-disposition"]='attachment; filename="export {{mconfig.name}}.json"';
				return JSON.stringify(table,undefined," ");
			case "xml":
				var data=this.F("Admin","json_table_to_xml",table);
				this.jsng_response.response_headers["Content-type"]="text/xml";
				this.jsng_response.response_headers["Content-disposition"]='attachment; filename="export {{mconfig.name}}.xml"';
				return data;
				break;
			case "xlsx":
				var data=this.F("Admin","json_table_to_xlsx",table);
				this.jsng_response.response_headers["Content-type"]="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
				this.jsng_response.response_headers["Content-disposition"]='attachment; filename="export {{mconfig.name}}.xlsx"';
				return data;
				break;
			default:
				throw "Unknown filetype";
		}
	}
}
